package com.fastwire.carriere.hibernate.daoimpl;

import java.util.HashMap;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.dao.CrossConnectBandwidthDao;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.inventory.entity.CrossConnect;
import com.fastwire.carriere.inventory.entity.CrossConnectBandwidth;

// TODO: Auto-generated Javadoc
/**
 * The Class CrossConnectBandwidthDaoImpl.
 */
public class CrossConnectBandwidthDaoImpl extends AbstractDAOImpl implements
		CrossConnectBandwidthDao {

	/* (non-Javadoc)
	 * @see com.fastwire.carriere.dao.CrossConnectBandwidthDao#getCrossConnectBandwidthbyName(java.lang.String)
	 */
	@Override
	public CrossConnectBandwidth getCrossConnectBandwidthbyName(String ccName)
			throws DAOException {

		Session session = HibernateUtil.openSession();
		//TODO: Later remove log message
		CommonLogger.info(this, "Querying CC BW from DB for : " + ccName);
		try {

			Criteria criteria = session
					.createCriteria(CrossConnectBandwidth.class);
			
			Criterion nameCriterion = Restrictions.eq("emsNativeName", ccName);

			criteria.add(nameCriterion);

			return (CrossConnectBandwidth) criteria.uniqueResult();

		} catch (Exception ex) {
			if (session.getTransaction().isActive()) {
				session.getTransaction().rollback();
			}
			throw new DAOException(ex);
		} finally {
			HibernateUtil.close(session);

		}
	}
	
	@Override
	public HashMap<String,CrossConnectBandwidth> getCrossConnectBandwidthbyList(List<String> allCCs)
			throws DAOException {

		Session session = HibernateUtil.openSession();
		//HashMap<String, CrossConnectBandwidth> ccBWMap = null;
		HashMap<String, CrossConnectBandwidth> ccBWMap = new HashMap<String, CrossConnectBandwidth>();
		List<CrossConnectBandwidth> allCCBWList = null;
		try {

			Criteria criteria = session
					.createCriteria(CrossConnectBandwidth.class);
			//Query with IN operator to get fast results. 
			Criterion nameCriterion = Restrictions.in("emsNativeName", allCCs);
			criteria.add(nameCriterion);

			allCCBWList = (List<CrossConnectBandwidth>) criteria.list();
			 
			//then re-package as key value pair 
			for (CrossConnectBandwidth ccBWType : allCCBWList)
			{
					String ccName = ccBWType.getEmsNativeName();
					ccBWMap.put(ccName, ccBWType);
			 }
			
			 return ccBWMap;

		} catch (Exception ex) {
			if (session.getTransaction().isActive()) {
				session.getTransaction().rollback();
			}
			throw new DAOException(ex);
		} finally {
			HibernateUtil.close(session);

		}
	}


	/* (non-Javadoc)
	 * @see com.fastwire.carriere.dao.CrossConnectBandwidthDao#findAllCCBandwisths()
	 */
	@Override
	public List findAllCCBandwisths() throws DAOException {
		Session session = HibernateUtil.openSession();

		try {

			Criteria criteria = session
					.createCriteria(CrossConnectBandwidth.class);

			return criteria.list();

		} catch (Exception ex) {
			if (session.getTransaction().isActive()) {
				session.getTransaction().rollback();
			}
			throw new DAOException(ex);
		} finally {
			HibernateUtil.close(session);

		}
	}

}
